// pages/edit/edit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploaderList: [], //已选择的图片临时路径数组
    uploaderNum: 0, //已选择的图片个数
    showUpload: true,
    scenicId:'',
    userId:'',
    content:'', 
    returnImgList:'',
  },
  clearImg: function (e) {
    var nowList = []; //新数据
    var uploaderList = this.data.uploaderList; //原数据

    for (let i = 0; i < uploaderList.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        continue;
      } else {
        nowList.push(uploaderList[i])
      }
    }
    this.setData({
      uploaderNum: this.data.uploaderNum - 1,
      uploaderList: nowList,
      showUpload: true
    })
  },
  //展示图片
  showImg: function (e) {
    var that = this;
    wx.previewImage({
      urls: that.data.uploaderList,
      current: that.data.uploaderList[e.currentTarget.dataset.index]
    })
  },
  //上传图片
  upload: function (e) {
    var that = this;
    wx.chooseImage({
      count: 4 - that.data.uploaderNum, // 默认4
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths)
        let uploaderList = that.data.uploaderList.concat(tempFilePaths);
        
        if (uploaderList.length == 4) {
          that.setData({
            showUpload: false
          })
        }
        that.setData({
          uploaderList: uploaderList,
          uploaderNum: uploaderList.length,
        })
        
        var length = res.tempFilePaths.length; //总数
        var count = 0; //第几张
        var returnImgL = []
        that.uploadOneByOne(res.tempFilePaths,count,length,returnImgL);
    //   wx.uploadFile({
    //      url: 'http://127.0.0.1:8082/travel/scenic/returnImgUrl', //仅为示例，非真实的接口地址
    //      filePath: that.data.uploaderList[0],
    //      name: 'file',
    //      formData: {
    //        scenicId: that.data.scenicId,
    //      },
    //      success: function (res) {
    //      var data = res.data
    //      console.log(data)
    //      //do something
    //     }
    // })
    }
  })
  },

    /**
  * 采用递归的方式上传多张
  */
 uploadOneByOne(imgPaths, count, length, returnImgL){
  var that = this;
  
  wx.uploadFile({
    url: 'http://120.25.151.125:8080/travel/scenic/returnImgUrl', //仅为示例，非真实的接口地址
    filePath: imgPaths[count],
    name: 'file',
    formData: {
       scenicId: that.data.scenicId,
    },
    success:function(res){
      returnImgL[count] = res.data
      console.log(returnImgL)
      count++
    },
    complete: function(e){
      if(count == length){
        //上传完毕，作一下提示
        console.log('上传成功');
        console.log(returnImgL)
        that.setData({
          returnImgList:returnImgL
        })
      }else{
        //递归调用，上传下一张
        that.uploadOneByOne(imgPaths, count, length, returnImgL);
      }
    }
  })
},

/** 
  push: function (e) {
    var that = this
    console.log(this.data.uploaderList)

    // wx.uploadFile({
    //   url: 'http://120.25.151.125:8080/travel/scenic/scenicClockIn', //仅为示例，非真实的接口地址
    //   filePath: this.data.uploaderList[0],
    //   name: 'file',
    //   formData: {
    //     content: this.data.content,
    //     userId: this.data.userId,
    //     scenicId: this.data.scenicId,
    //   },
    //   success: function (res) {
    //     var data = res.data
    //     console.log(data)
    //     //do something
    //   }
    // })
    
    wx.navigateTo({
      url: '/pages/attractionsDetails/attractionsDetails?scenicId='+this.data.scenicId
      })
      
    
      // var pages = getCurrentPages(); //当前页面
      // var beforePage = pages[pages.length - 2]; //前一页
      // wx.navigateBack({
      //   success: function () {
      //     beforePage.onLoad(); // 执行前一个页面的onLoad方法
      //   }
      // });
    

  },*/

  
  push:function(){
    var scenicId = this.data.scenicId
    console.log(this.data.returnImgList)
    wx.request({
      url: 'http://120.25.151.125:8080/travel/scenic/scenicClockIn',
      data:{
        content:this.data.content,
        userId:this.data.userId,
        //imgUrl:this.data.returnImgList,
        scenicId:scenicId
      },
      method:'GET',
      header:{
        'Content-Type': 'application/json'
      },
  })

  wx.navigateTo({
    url: '/pages/attractionsDetails/attractionsDetails?scenicId='+scenicId,
  })

},

getcontent:function(e){
  var that = this
  that.setData({
      content:e.detail.value
  })
},



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
   
    that.setData({
        scenicId:options.scenicId,
        userId:options.userId,
    })

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})